package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.SmModule;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISmModuleDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.ISmModuleService;
import com.huitone.gddw.service.dto.SmModuleDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短接模板接口
 * @author Weiliyu
 *
 */
@Service
public class SmModuleService implements ISmModuleService {

	@Autowired
	private ISmModuleDao smModuleDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<SmModuleDTO> selectPage(QueryPage<SmModule> page) {
		Map<String, Object> map = new HashMap<>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("smTemplet", page.getParams().get("smTemplet"));
		map.put("startDate", page.getParams().get("startDate"));
		map.put("endDate", page.getParams().get("endDate"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SmModuleDTO> result = smModuleDao.selectPage(map);
		int totalCount = smModuleDao.selectCount(map);
		
		QueryPage<SmModuleDTO> queryPage = new QueryPage<>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public SmModule selectByPrimaryKey(String smTempletId) {
		return smModuleDao.selectByPrimaryKey(smTempletId);
	}
	
	@Override
	public void save(SmModule entity) {
		entity.setStatus("1"); //默认有效
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		smModuleDao.insertSelective(entity);
		this.updateParamRec("保存短信模板信息");
	}

	@Override
	public void update(SmModule entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		smModuleDao.updateByPrimaryKey(entity);
		this.updateParamRec("更新短信模板信息");
	}

	@Override
	public SmModule toUpdate(String smTempletId) {
		return this.selectByPrimaryKey(smTempletId);
	}

	@Override
	public void delete(String smTempletId) {
		smModuleDao.deleteByPrimaryKey(smTempletId);
		this.updateParamRec("删除短信模板信息");
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SM_MODULE, desc);
	}
}
